package com.woniuxy.woniuhis.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.SurgeryConfirm;
import com.woniuxy.woniuhis.entity.SurgeryEmpArr;
import com.woniuxy.woniuhis.entity.SurgeryExe;
import com.woniuxy.woniuhis.exception.SurgeryExeServiceException;
import com.woniuxy.woniuhis.mapper.SurgeryConfirmMapper;
import com.woniuxy.woniuhis.mapper.SurgeryEmpArrMapper;
import com.woniuxy.woniuhis.mapper.SurgeryExeMapper;
import com.woniuxy.woniuhis.service.SurgeryExeService;

@Service
public class SurgeryExeServiceImpl implements SurgeryExeService{
	
	@Autowired
	SurgeryExeMapper surgeryExeMapper;
	
	@Autowired
	SurgeryEmpArrMapper surgeryEmpArrMapper;
	
	@Autowired
	SurgeryConfirmMapper surgeryConfirmMapper;
	
	/**
	 * 	添加手术执行记录
	 * 	
	 */
	@Override
	@Transactional
	public void addSurgeryExe(SurgeryExe surgeryExe) {
		try {
			// 插入执行记录
			surgeryExeMapper.insertSurgeryExe(surgeryExe);
			// 插入手术的人员安排
			if(surgeryExe.getSurgeryEmpArrs()!=null && surgeryExe.getSurgeryEmpArrs().size()!=0) {
				for(SurgeryEmpArr empArr:surgeryExe.getSurgeryEmpArrs()) {
					surgeryEmpArrMapper.insertSurgeryEmpArr(empArr);
				}
			}
			// 插入手术同意书
			if(surgeryExe.getSurgeryConfirm()!= null) {
				surgeryConfirmMapper.insertSurgeryConfirm(surgeryExe.getSurgeryConfirm());
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryExeServiceException("系统维护中");
		}
		
	}
	
	/**
	 * 	更新手术执行记录
	 */
	@Override
	public void modifySurgeryExe(SurgeryExe surgeryExe) {
		// 手术执行记录的更新，包括本表的更新，
		// 手术人员安排，单独提供更新操作
		// 修改人员安排，在已经存在的人员安排里面，两种操作，一种全部删除，全部插入，另一种差异更新，
		// 手术同意书的更新，单独提供更新操作
		// 这里只考虑执行表的更新
		try {
			surgeryExeMapper.updateSurgeryExe(surgeryExe);
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryExeServiceException("系统维护中");
		}
	}
	
	/**
	 * 	查询指定手术执行记录详情
	 */
	@Override
	public SurgeryExe findSurgeryExeById(String id) {
		try {
			// 查询执行记录
			SurgeryExe surgeryExe = surgeryExeMapper.selectSurgeryExeById(id);
			// 查询手术人员安排
			List<SurgeryEmpArr> surgeryEmpArrs = surgeryEmpArrMapper.selectSurgeryEmpArrBySurgeryId(id);
			surgeryExe.setSurgeryEmpArrs(surgeryEmpArrs);
			// 查询手术同意书的信息
			SurgeryConfirm surgeryConfirm = surgeryConfirmMapper.selectSurgeryConfirmsBySurgeryExeId(surgeryExe.getSurgeryExeId());
			surgeryExe.setSurgeryConfirm(surgeryConfirm);
			return surgeryExe;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryExeServiceException("系统维护中");
		}
	}
	
	/**
	 * 	查询所有的手术执行记录
	 */
	@Override
	public PageInfo<SurgeryExe> findSurgeryExes(SurgeryExe surgeryExe, int pageIndex, int pageSize) {
		try {
			PageHelper.startPage(pageIndex, pageSize);
			List<SurgeryExe> surgeryExes = surgeryExeMapper.selectSurgeryExes(surgeryExe);
			for(SurgeryExe item:surgeryExes) {
				item = findSurgeryExeById(item.getSurgeryExeId());
			}
			PageInfo<SurgeryExe> pageInfo = new PageInfo<SurgeryExe>(surgeryExes);
			return pageInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryExeServiceException("系统维护中");
		}
	}
	
	/**
	 * 	查询所有的手术执行记录
	 */
	@Override
	public List<SurgeryExe> findAllSurgeryExes(SurgeryExe surgeryExe) {
		try {
			List<SurgeryExe> surgeryExes = surgeryExeMapper.selectSurgeryExes(surgeryExe);
			for(SurgeryExe item:surgeryExes) {
				item = findSurgeryExeById(item.getSurgeryExeId());
			}
			return surgeryExes;
		} catch (Exception e) {
			e.printStackTrace();
			throw new SurgeryExeServiceException("系统维护中");
		}
	}

}
